
const app = getApp()
Page({
  data: {
    active: 0,
    showSalePop: false,
    result: [],
    good_list: [],
    number: 0,
    freight: 0,
    saleValue: null,
    awardPost: {},
    currentMall: {},
    pageNum: 1,
  },
  onLoad: function (options) {
    if (options.active) {
      let active=JSON.parse(options.active)
      this.setData({
        active: active
      })
    }
    this.setData({
      good_list:[]
    })
    this.getMyGoodsList()
    this.getFreight()
  },
  onUnload(){
    wx.switchTab({
      url: '/pages/personal/personal-index',
    })
  },
  changeStatus(event) {
    this.setData({
      active: event.detail.index, 
      result: [],
      good_list: [],
      pageNum: 1,
      number:0
    })
    this.getMyGoodsList()
  },
  getMyGoodsList() {
    let _this = this
    wx.showLoading({
      title: '加载中',
    })
    app.util.request({
      url: "api/award/findLimit",
      method: "post",
      data: {
        state: _this.data.active, 
        page: _this.data.pageNum,
        pageSize: 10,
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            good_list: [..._this.data.good_list,...response.data.data.list]
          })
          if(_this.data.pageNum<response.data.data.pages){
            _this.data.pageNum++
            _this.getMyGoodsList()
          }else{
            wx.hideLoading({
              success: (res) => {},
            })
          }
        }
      }
    })
  },
  onChange(event) {
    console.log(event)
    this.setData({
      result: event.detail,
      number: event.detail.length,
    });
    this.getFreight(event.detail)
  },
  toggle(event) {
    let arr = this.data.result;
    let tempId = String(event.currentTarget.dataset.id)

    if (arr.length === 0) {
      arr.push(tempId)
    } else {
      if (arr.indexOf(tempId) != -1) {
        let index = arr.indexOf(tempId)
        arr.splice(index, 1)
      } else {
        arr.push(tempId)
      }
    }
    this.setData({
      result: arr,
      number: arr.length,
    });
    this.getFreight(arr)
    return
    const {index} = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  getFreight(id) {
    let _this = this
    app.util.request({
      url: "api/award/postage",
      method: "post",
      data: {
        id: id
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            awardPost: response.data.data[0]
          })
        }
      }
    })
  },
  noop() {},
  toDeal(e) {
    if (this.data.number > 1 || this.data.number == 0) {
      wx.showToast({
        icon: 'none',
        title: '仅可选择一项',
      })
    } else if (e.currentTarget.dataset.type == 0) {
      wx.navigateTo({
        url: '/pages/market/change-goods/change-goods?id=' + this.data.result,
      })
    } else {
      this.setData({
        currentMall: this.data.good_list.find(x => x.id == this.data.result[0]),
        showSalePop: true
      })
    }
  },
  onClose() {
    this.setData({
      showSalePop: false
    })
  },
  changeSaleValue(e) {
    this.setData({
      saleValue: e.detail.value
    })
  },
  toSale() {
    let _this = this
    app.util.request({
      url: "api/deal/add",
      method: "post",
      data: {
        awardId: _this.data.currentMall.id,
        price: _this.data.saleValue,
        state: 1
      },
      success(response) {
        if (response.data.code == 0) {
          wx.showToast({
            title: '寄售成功',
          })
          _this.onClose()
          _this.setData({
            good_list:[]
          })
          _this.getMyGoodsList()
        }
      }
    })
  },
  // 立即发货
  toSend() {
    let _this = this
    console.log(this.data.result)
    let res = this.data.result.toString()
    if (_this.data.result.length > 0) {
      app.util.request({
        url: "api/award/isshipments",
        method: "post",
        data: {
          id: res
        },
        success(response) {
          if (response.data.code == 200) {
            wx.navigateTo({
              url: '/pages/order/shipments/shipments?id=' + _this.data.result + '&price=' + _this.data.awardPost.price + '&active=' + 2,
            })
          } else {
            wx.showModal({
              title: '提示',
              content: response.data.msg,
              showCancel: false,
            })
          }
        }
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '请选择商品',
      })
    }
  },
})
